package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    //使用("/login")来接收前端发送的请求
    @RequestMapping("/login")
    //用于接受前端传送过来的json类型的数据
    public R<Employee> login(HttpSession session, @RequestBody Employee employee){
        //创建条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //通过名字在数据库中查找
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lqw);
        //判断查找结果是否为空，若为空则证明用户没有被添加
        if (emp == null){
            return R.error("用户不存在");
        }

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        if (!emp.getPassword().equals(password)){
            return R.error("用户密码错误");
        }
        if (emp.getStatus()==0){
            return R.error("用户已经禁用");
        }

        session.setAttribute("employee",emp);
        return R.success(emp);
    }

    /**
     * 员工退出
     * @param session
     * @return
     */
    @PostMapping("logout")
    public R<String> loginout(HttpSession session){
        session.removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加员工信息
     * @param session
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpSession session,@RequestBody Employee employee){

        long id = Thread.currentThread().getId();
        log.info("线程---"+id);

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

//        Employee emp = (Employee) session.getAttribute("employee");

//        employee.setCreateUser(emp.getId());
//        employee.setUpdateUser(emp.getId());

        employeeService.save(employee);
        return R.success("添加员工成功");
    }


    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Employee> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),Employee::getUsername,name);
        lqw.orderByDesc(Employee::getCreateTime);
        employeeService.page(page1,lqw);
        return R.success(page1);
    }

    /**
     * 通过id修改员工信息
     */
    @PutMapping
    public R<String> update(HttpSession session, @RequestBody Employee employee){
        //获得登录人的对象
//        Employee emp = (Employee) session.getAttribute("employee");
        //获得修改时间
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(emp.getId());
        //调用方法修改数据
        employeeService.updateById(employee);
        return R.success("修改用户信息成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getId(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
        return R.error("没有对应员工信息");
    }
}
